package org.kisekiproject.evaluator {
	import flash.events.Event;
	import org.kisekiproject.utils.StringUtil;
	import flash.events.EventDispatcher;
	import org.kisekiproject.evaluator.IExpression;

	/**
	 * @author mikael
	 */
	public class BindingExpression extends EventDispatcher implements IExpression {

		private var _expression:Expression;
		private var _constantValue:String;

		/**
		 * Constructor.
		 */
		public function BindingExpression(expression:String, variables:IVariableNamespace) {
			var s:String=StringUtil.trim(expression);
			//var s:String=expression;

			if (s.charAt(0)=="{" && s.charAt(s.length-1)=="}") {
				_expression=new Expression(s.substr(1,s.length-2), variables);
				_expression.addEventListener(Event.CHANGE, onExpressionChange);
			}

			else
				_constantValue=expression;
		}

		/**
		 * Change.
		 */
		private function onExpressionChange(event : Event) : void {
			dispatchEvent(new Event(Event.CHANGE));
		}

		/**
		 * Get value.
		 */
		public function get value():Object {
			if (_expression)
				return _expression.value;

			return _constantValue;
		}
	}
}
